import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../app_response.dart';

class ResponseInterceptor extends InterceptorsWrapper {
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) async {
    int code = response.statusCode!;
    if (code >= 200 &&
        code < 300 &&
        response.data != null &&
        response.data['code'] == 0) {
      return handler.resolve(Response(
          requestOptions: response.requestOptions,
          data: AppResponse.success(response.data['data'])));
    }
    if (response.data != null && response.data['code'] != 0) {
      // throw Exception(response.data['msg']);
      EasyLoading.showError(response.data['msg']);
    } else {
      return handler.resolve(Response(
          requestOptions: response.requestOptions,
          data: AppResponse.failure(response.statusMessage)));
    }
  }
}
